using System;
using System.Collections;
using System.Collections.Generic;
using UMC.Data;
using UMC.Web;
using UMC.WorkFlow.Entities;

namespace UMC.WorkFlow
{


    [Apiumc("WorkFlow", "Form", Auth = WebAuthType.Admin, Desc = "表单设计")]
    public class WorkFlowFormActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var SId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
            {
                this.Prompt("无设置流程");
                return this.DialogValue("none");
            })) ?? 0;

            var vform = this.AsyncDialog("TaskForm", g =>
            {

                var taskForm = DataFactory.Instance().TaskForm(SId) ?? new TaskForm();
                var form = new UIFormDialog() { Title = "流程表单" };


                if (taskForm.TaskId.HasValue)
                {
                    form.AddTextValue().Put("表单代码", taskForm.Code)
                    .Put("表单名称", taskForm.Caption);
                    form.AddText("标题格式", "SubjectFormat", taskForm.SubjectFormat).NotRequired();
                    form.AddText("编码前缀", "PfxFormat", taskForm.PfxFormat).NotRequired();
                    form.AddRadio("编码时间格式", "DateFormat")
                    .Put("长年月日", "yyyyMMdd", taskForm.DateFormat == "yyyyMMdd")
                    .Put("短年月日", "yyMMdd", taskForm.DateFormat == "yyyyMMdd");
                    form.AddRadio("编码增量方式", "TotalFormat")
                    .Put("按年", "Y", taskForm.TotalFormat == "Y")
                    .Put("按月", "M", taskForm.TotalFormat == "M")
                    .Put("按天", "D", taskForm.TotalFormat == "D");
                    form.AddOption("表单数据库", "ProviderKey", taskForm.ProviderKey, taskForm.ProviderKey).Command(this.Context.Request.Model, "DbProvider");

                }
                else
                {
                    form.Title = "新建表单";
                    form.AddText("表单代码", "Code", taskForm.Code);
                    form.AddText("表单名称", "Caption", taskForm.Caption);
                }

                form.Submit("确认", "WorkFlow.Form", "WorkFlow.Design");

                return form;
            });
            var tf = new TaskForm();
            UMC.Data.Reflection.SetProperty(tf, vform.GetDictionary());
            if (SId == 0)
            {
                if (DataFactory.Instance().TaskForm(tf.Code)!=null)//.Count() > 0)
                {
                    this.Prompt("表单代码，已经存在");
                }
                tf.TaskId = Utility.NewId();
                DataFactory.Instance().Put(tf);
                response.Redirect(request.Model, "Open", tf.TaskId.ToString());
            }
            else
            {
                DataFactory.Instance().Put(tf);
            }

            this.Context.Send("WorkFlow.Form", true);
        }

    }

}